﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class InventoryDetailDAO
    {
        DataServices m_DetailDAO = new DataServices();

        public DataTable GetAll()
        {
            m_DetailDAO.Load("GetAllInventoryDetail", null);
            return m_DetailDAO;
        }

        public DataTable GetByInventoryId(String inventoryId)
        {
            SqlParameter param = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param.Value = inventoryId;

            m_DetailDAO.Load("GetInventoryDetailByInventoryId", param);
            return m_DetailDAO;
        }

        public DataTable GetByInventoryIdFull(String inventoryId)
        {
            SqlParameter param = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param.Value = inventoryId;

            m_DetailDAO.Load("GetInventoryDetailByInventoryIdFull", param);
            return m_DetailDAO;
        }

        public DataTable GetByInventoryAndMaterial(String inventoryId, String materialId)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = inventoryId;

            param[1] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[1].Value = materialId;

            m_DetailDAO.Load("GetInventoryDetailByInventoryAndMaterial", param);
            return m_DetailDAO;
        }

        public int Add(InventoryDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[1].Value = info.MaterialId;

            param[2] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[2].Value = info.Quantity;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = info.UnitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[5].Value = info.IsDeleted;

            return m_DetailDAO.ExecuteUpdate("AddInventoryDetail", param);
        }

        public int Update(InventoryDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[1].Value = info.MaterialId;

            param[2] = new SqlParameter("@Quantity", SqlDbType.Int);
            param[2].Value = info.Quantity;

            param[3] = new SqlParameter("@UnitId", SqlDbType.VarChar, 6);
            param[3].Value = info.UnitId;

            param[4] = new SqlParameter("@Price", SqlDbType.Money);
            param[4].Value = info.Price;

            param[5] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[5].Value = info.IsDeleted;

            return m_DetailDAO.ExecuteUpdate("UpdateInventoryDetail", param);
        }

        public int Delete(InventoryDetailInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InventoryId", SqlDbType.VarChar, 14);
            param[0].Value = info.InventoryId;

            param[1] = new SqlParameter("@MaterialId", SqlDbType.VarChar, 6);
            param[1].Value = info.MaterialId;

            return m_DetailDAO.ExecuteUpdate("DeleteInventoryDetail", param);
        }

        public DataRow AddNewRow()
        {
            return m_DetailDAO.NewRow();
        }

        public void AddNewInventoryDetail(DataRow m_Row)
        {
            m_DetailDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_DetailDAO.ExecuteNoneQuery() > 0;
        }
    }
}
